package com.atguigu.tingshu.order.handle;

import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Timer;

/**
 * @author atguigu-mqx
 * @ClassName RedisDelayHandle
 * @description: TODO
 * @date 2024年01月06日
 * @version: 1.0
 */
@Component
@EnableScheduling
public class RedisDelayHandle {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderInfoService orderInfoService;



    /**
     * 定时任务：后续升级：xxl-job 分布式定时调度框架.
     */
    //    @Scheduled(cron = "0/1 * * * * ?")
    //    public void cancel(){
    //        System.out.println("78iop[");
    //    }

    //  监听消息。init()
    @PostConstruct
    public void orderCancel(){

        //  阻塞队列
        RBlockingQueue<Object> blockingQueue = redissonClient.getBlockingQueue(KafkaConstant.QUEUE_ORDER_CANCEL);
        //  开一个线程：
        new Thread(()->{
            while (true){
                String orderId = null;
                try {
                    orderId = (String) blockingQueue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                if (!StringUtils.isEmpty(orderId)){
                    orderInfoService.orderCancel(orderId);
                }
            }
        }).start();
    }

}
